var myChart1 = echarts.init(document.getElementById("shuzhileiji"));
var myChart2 = echarts.init(document.getElementById("yusuan-chart"));
var myChart3 = echarts.init(document.getElementById("zhichu-chart"));
var myChart4 = echarts.init(document.getElementById("buemnejianjie-chart"));
$(function () {
  DSSindex = 1;
  initDDSindexData();

  document.getElementById("shouzhi").style.display = "none";
  document.getElementById("xiangmu").style.display = "none";
  document.getElementById("zhichuzucheng").style.display = "none";
  document.getElementById("bnemejianjie").style.display = "none";
})

// $(".content").resize(function () {
//   myChart1.resize();
//   myChart2.resize();
//   myChart3.resize();
//   myChart4.resize();
// });
// $("div").resize(function () {
//   myChart1.resize();
//   myChart2.resize();
//   myChart3.resize();
//   myChart4.resize();
// });

function initDDSindexData() {
  const username = localStorage.getItem("user");
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");
  //权限
  var obj = {
    username: username,
    // budgetYear: currYear,
    // budgetMonth: currMon,
    // dept: currDept,
    status: 2,
    chartPage: 704,
    chartNo: 1,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    console.log(chartData, "权限");
    if (chartData[0].key07 == '1') {
      document.getElementById("shouzhi").style.display = "block";
    }
    if (chartData[0].key08 == '1') {
      document.getElementById("xiangmu").style.display = "block";
    }
    if (chartData[0].key09 == '1') {
      document.getElementById("zhichuzucheng").style.display = "block";
    }
    if (chartData[0].key10 == '1') {
      document.getElementById("bnemejianjie").style.display = "block";
    }

  });
  //1
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 6,
    chartNo: 1,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    console.log(chartData, "全所收支累计--数字");
    let lastmonthShoukuan = chartData[0].key02.indexOf("-") //不包含-1  包含返回索引
    let lastmonthZhichu = chartData[0].key04.indexOf("-") //不包含-1  包含返回索引

    // if (lastmonthShoukuan == -1) {
    //   console.log('正数')
    //   $('#than').html(`<div class="name-bg_up">
    //   <span>&#9650;</span> <span class="lastmonth-shoukuan"> </span>
    // </div>
    // 较上月`)
    // } else {
    //   console.log('负数')
    //   $('#than').html(`<div class="name-bg_down">
    //   <span>&#9660;</span> <span class="lastmonth-shoukuan"></span>
    // </div>
    // 较上月`)
    // }
    // if (lastmonthZhichu == -1) {
    //   console.log('正数')
    //   $('#than0').html(`<div class="name-bg_up">
    //   <span>&#9650;</span> <span class="lastmonth-zhichu"> </span>
    // </div>
    // 较上月`)
    // } else {
    //   console.log('负数')
    //   $('#than0').html(`<div class="name-bg_down">
    //   <span>&#9660;</span> <span class="lastmonth-zhichu"></span>
    // </div>
    // 较上月`)
    // }
	
	$('.monthNum0').html(`<div class="">
												<h4>上月：</h4>
												<p>${FormatWYCash(chartData[0].key07?chartData[0].key07:0)}<span> 万元</span></p>
											</div>
											<div class="">
												<h4>本月：</h4>
												<p>${FormatWYCash(chartData[0].key05?chartData[0].key05:0)}<span> 万元</span></p>
											</div>`)
	$('.monthNum1').html(`<div class="">
												<h4>上月：</h4>
												<p>${FormatWYCash(chartData[0].key08?chartData[0].key08:0)}<span> 万元</span></p>
											</div>
											<div class="">
												<h4>本月：</h4>
												<p>${FormatWYCash(chartData[0].key06?chartData[0].key06:0)}<span> 万元</span></p>
											</div>`)

    $(".benyueshoukuan").text(pricefmt((chartData[0].key01 / 10000).toFixed(2)));
    $(".benyuezhichu").text(pricefmt((chartData[0].key03 / 10000).toFixed(2)));
    $(".lastmonth-shoukuan").text((chartData[0].key02 * 100).toFixed(1) + '%');
    $(".lastmonth-zhichu").text((chartData[0].key04 * 100).toFixed(1) + "%");

  });
  //2
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 6,
    chartNo: 2,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    // console.log(chartData, "全所收支累计--图表");
    getshouzhileijiChart(chartData) //全所收支累计
  });

  //全所项目预算执行累计情况
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 6,
    chartNo: 3,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;

    $(".ProjectCollectionOfMonth").text(pricefmt((chartData[0].key01 / 10000).toFixed(2)));
    $(".BudgetOfMonth").text(pricefmt((chartData[0].key03 / 10000).toFixed(2)));
    $(".CostExpenditureOfMonth").text(pricefmt((chartData[0].key05 / 10000).toFixed(2)));

    let thanShoukuan = chartData[0].key02.indexOf("-")
    let thanYuesuan = chartData[0].key04.indexOf("-")
    let thanZhichu = chartData[0].key06.indexOf("-")
    if (thanShoukuan == -1) {
      console.log('正数')
      $('#than1').html(`<div class="name-bg_up">
      <span>&#9650;</span> <span> ${Percenting(chartData[0].key02)}%</span>
    </div>
    较上月`)
    } else {
      console.log('负数')
      $('#than1').html(`<div class="name-bg_down">
      <span>&#9660;</span> <span> ${Percenting(chartData[0].key02)}%</span>
    </div>
    较上月`)
    }
    if (thanYuesuan == -1) {
      console.log('正数')
      $('#than2').html(`<div class="name-bg_up">
      <span>&#9650;</span> <span> ${Percenting(chartData[0].key04)}%</span>
    </div>
    较上月`)
    } else {
      console.log('负数')
      $('#than2').html(`<div class="name-bg_down">
      <span>&#9660;</span> <span> ${Percenting(chartData[0].key04)}%</span>
    </div>
    较上月`)
    }
    if (thanZhichu == -1) {
      console.log('正数')
      $('#than3').html(`<div class="name-bg_up">
      <span>&#9650;</span> <span> ${Percenting(chartData[0].key06)}%</span>
    </div>
    较上月`)
    } else {
      console.log('负数')
      $('#than3').html(`<div class="name-bg_down">
      <span>&#9660;</span> <span> ${Percenting(chartData[0].key06)}%</span>
    </div>
    较上月`)
    }
  });
  // 全所项目预算执行累计情况
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60101,
    chartNo: 16,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    console.log(chartData, "项目成本预算执行情况");
    getyusuanleijiChart(chartData)
  });

  //全所支出组成
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 6,
    chartNo: 5,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;

    let thanZhichuAll = chartData[0].key02.indexOf("-")
    if (thanZhichuAll == -1) {
      console.log('正数')
      $('#than4').html(`<div class="name-bg_up">
      <span>&#9650;</span> <span class="lastmonth-yusuan"> </span>
    </div>
    较上月`)
    } else {
      console.log('负数')
      $('#than4').html(`<div class="name-bg_down">
      <span>&#9660;</span> <span class="lastmonth-yusuan"> </span>
    </div>
    较上月`)
    }
    getzhichuzuchengChart(chartData); //全所支出组成
  });
  //部门间接费用
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 6,
    chartNo: 6,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    // console.log(chartData, "部门间接费用--数字");
    let thanbumenjianjie = chartData[0].key02.indexOf("-")
    if (thanbumenjianjie == -1) {
      console.log('正数')
      $('#than5').html(`<div class="name-bg_up">
      <span>&#9650;</span> <span class="lastmonth-jianjie"> </span>
    </div>
    较上月`)
    } else {
      console.log('负数')
      $('#than5').html(`<div class="name-bg_down">
      <span>&#9660;</span> <span class="lastmonth-jianjie"> </span>
    </div>
    较上月`)
    }
    $(".jianjieall").text(pricefmt((chartData[0].key01 / 10000).toFixed(2)));
    $(".lastmonth-jianjie").text((chartData[0].key02 * 100).toFixed(1) + '%');
    $(".gongziNum-jianjie").text(pricefmt((chartData[0].key03 / 10000).toFixed(2)));
    $(".yanzhiNum-jianjie").text(pricefmt((chartData[0].key04 / 10000).toFixed(2)));
    $(".guanliNum-jianjie").text(pricefmt((chartData[0].key05 / 10000).toFixed(2)));
  });
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 6,
    chartNo: 7,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    // console.log(chartData, "部门间接费用--图表");
    getbumenjianjieChart(chartData); //部门间接费用
  });
}

function getshouzhileijiChart(data) {
  console.log('111111')
  let legendData = ['项目收款', '成本支出']
  let xiangmushoukuan = [];
  let chengbenzhichu = [];
  for (let i = 0; i < data.length; i++) {
    xiangmushoukuan.push((data[i].key02 / 10000).toFixed(2))
    chengbenzhichu.push((data[i].key03 / 10000).toFixed(2))
  }
  let leijirate = [{
    name: '项目收款',
    smooth: true, //平滑曲线显示
    symbol: 'circle', //标记的图形为实心圆
    symbolSize: 8, //标记的大小
    itemStyle: {
      normal: {
        color: '#165DFF',
      },
    },
    showSymbol: false,
    type: 'line',
    lineStyle: {
      width: 2
    },
    areaStyle: {
      color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
        offset: 0,
        color: 'rgba(230,238,255,0.8)'
      },
      {
        offset: 1,
        color: 'rgba(230,238,255,0.2)'
      }
      ])
    },
    data: xiangmushoukuan
  },

  {
    name: '成本支出',
    smooth: true, //平滑曲线显示
    symbol: 'circle', //标记的图形为实心圆
    symbolSize: 8, //标记的大小
    itemStyle: {
      normal: {
        color: '#20C997',
      },
    },
    showSymbol: false,
    lineStyle: {
      width: 2
    },
    areaStyle: {
      color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
        offset: 0,
        color: 'rgba(212,245,245,0.8)'
      },
      {
        offset: 1,
        color: 'rgba(212,245,245,0.2)'
      }
      ])
    },
    type: 'line',
    data: chengbenzhichu
  }
  ]
  have('shuzhileiji') ? lineChartMD('shuzhileiji', leijirate, legendData) : ''
}

function getyusuanleijiChart(data) {
  console.log('22222')

  let legendData = ['项目收款', '成本预算', '成本支出']
  let xiangmushoukuan = [];
  let chengbenyusuan = [];
  let chengbenzhichu = [];
  for (let i = 0; i < data.length; i++) {
    xiangmushoukuan.push((data[i].key02 / 10000).toFixed(1))
    chengbenyusuan.push((data[i].key03 / 10000).toFixed(1))
    chengbenzhichu.push((data[i].key04 / 10000).toFixed(1))
  }
  let leijirate = [{
    name: '项目收款',
    smooth: true, //平滑曲线显示
    symbol: 'circle', //标记的图形为实心圆
    symbolSize: 8, //标记的大小
    itemStyle: {
      normal: {
        color: '#165DFF',
      },
    },
    showSymbol: false,
    type: 'line',
    lineStyle: {
      width: 2
    },
    areaStyle: {
      color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
        offset: 0,
        color: 'rgba(230,238,255,0.8)'
      },
      {
        offset: 1,
        color: 'rgba(230,238,255,0.2)'
      }
      ])
    },
    data: xiangmushoukuan
  },
  {
    name: '成本预算',
    smooth: true, //平滑曲线显示
    symbol: 'circle', //标记的图形为实心圆
    symbolSize: 8, //标记的大小
    itemStyle: {
      normal: {
        color: '#F7BA1E',
      },
    },
    showSymbol: false,
    lineStyle: {
      width: 2
    },
    type: 'line',
    areaStyle: {
      color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
        offset: 0,
        color: 'rgba(254,244,218,0.8)'
      },
      {
        offset: 1,
        color: 'rgba(254,244,218,0.2)'
      }
      ])
    },
    data: chengbenyusuan
  },
  {
    name: '成本支出',
    smooth: true, //平滑曲线显示
    symbol: 'circle', //标记的图形为实心圆
    symbolSize: 8, //标记的大小
    itemStyle: {
      normal: {
        color: '#20C997',
      },
    },
    showSymbol: false,
    lineStyle: {
      width: 2
    },
    type: 'line',
    areaStyle: {
      color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
        offset: 0,
        color: 'rgba(212,245,245,0.8)'
      },
      {
        offset: 1,
        color: 'rgba(212,245,245,0.8)'
      }
      ])
    },
    data: chengbenzhichu
  }
  ]
  have('yusuan-chart') ? lineChartMD('yusuan-chart', leijirate, legendData) : ''
}

function getzhichuzuchengChart(data) {
  console.log('33333')

  $(".benyueyusuan").text(pricefmt((data[0].key01 / 10000).toFixed(2)));
  $(".lastmonth-yusuan").text((data[0].key02 * 100).toFixed(1) + '%');
  //占比
  $(".zongxiang").text((data[0].key03 * 100).toFixed(1) + "%");
  $(".yanzhi").text((data[0].key04 * 100).toFixed(1) + "%");
  $(".gongzi").text((data[0].key05 * 100).toFixed(1) + "%");
  $(".hengxiang").text((data[0].key06 * 100).toFixed(1) + "%");
  $(".guanli").text((data[0].key07 * 100).toFixed(1) + "%");
  //数字
  $(".zongxiangNum").text(pricefmt((data[0].key08 / 10000).toFixed(2)));
  $(".yanzhiNum").text(pricefmt((data[0].key09 / 10000).toFixed(2)));
  // $(".gongziNum").text(pricefmt((data[0].key10 / 10000).toFixed(2)));
  $(".hengxiangNum").text(pricefmt((data[0].key11 / 10000).toFixed(2)));
  $(".guanliNum").text(pricefmt((data[0].key12 / 10000).toFixed(2)));



  let yuyanChartData = [{
    value: data[0].key08,
    name: '纵向开支'
  },
  {
    value: data[0].key09,
    name: '部门研制费'
  },
  // {
  //   value: data[0].key10,
  //   name: '部门工资'
  // },
  {
    value: data[0].key11,
    name: '横向开支'
  },
  {
    value: data[0].key12,
    name: '部门管理费'
  },
  ]
  let yuyanChart = {
    tooltip: {
      trigger: 'item'
    },
    series: [{
      type: 'pie',
      radius: '94%',
      avoidLabelOverlap: false,
      itemStyle: {
        borderColor: '#fff',
        borderWidth: 2
      },
      label: {
        normal: {
          show: false,
        }
      },
      labelLine: {
        normal: {
          show: false
        }
      },
      color: [
        '#165DFF',
        '#50CD89',
        '#722ED1',
        '#FD7E14',
        '#0DCAF0',
      ],
      data: yuyanChartData,
      emphasis: {
        itemStyle: {
          shadowBlur: 2,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        }
      }
    }]
  };
  myChart3.setOption(yuyanChart);
  window.addEventListener("resize", function () {
    myChart3.resize();
  });
  $("div").resize(function () {
    myChart3.resize();
  });
}

function getbumenjianjieChart(data) {
  console.log('444444')

  let legendData = ['工资及福利费用', '研制费用', '管理费用'];
  let xData = []; //部门名称
  let gongzi = []; //工资及福利费用
  let yanzhi = []; //yanzhi
  let guanli = []; //管理

  for (let i = 0; i < data.length; i++) {
    xData.push(data[i].key01)
    gongzi.push(((data[i].key02) / 10000).toFixed(2))
    yanzhi.push(((data[i].key03) / 10000).toFixed(2))
    guanli.push(((data[i].key04) / 10000).toFixed(2))
  }
  let seriesData = [{
    name: '工资及福利费用',
    type: 'bar',
    stack: 'total', // ! 多条数据总计 => 堆叠
    barWidth: 30,
    color: '#165DFF',
    data: gongzi,
  },
  {
    name: '研制费用',
    type: 'bar',
    stack: 'total', // ! 多条数据总计 => 堆叠
    barWidth: 30,
    color: '#50CD89',
    data: yanzhi,
  },
  {
    name: '管理费用',
    type: 'bar',
    stack: 'total', // ! 多条数据总计 => 堆叠
    barWidth: 30,
    color: '#F7BA1E',
    data: guanli,
  },
  ]
  have('buemnejianjie-chart') ? getYstackedColumn('buemnejianjie-chart', xData, legendData, seriesData) : ''
}

function Percenting(val) {
  return (val * 100).toFixed(1)
}